#!/usr/bin/env python
#  Copyright (C) 2008 Monadsphere <monadli@gmail.com>
#
# This file is part of Monadsphere.
#
# Monadsphere is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Monadsphere is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Monadsphere. If not, see <http://www.gnu.org/licenses/>.

from monadsphere.core import *
from monadsphere.util import *
from monadsphere.expr import *

class GoogleGears(Component):

  def createHandle(self, val = None):
    pass

  def setOptions(self, options = None):
    if (options is not None):
      if ("src" in options):
        self.setSrc(options["src"])

  def setCallback(self, callBack):
    print 'var ' + self + '=function(){'
    callBack()
    print '}'

  def load(self):
    print 'monadsphere.loadScript("google/gears_init.js",' + self + ');'

  def createLocalServer(self):
    ls = LocalServer(self.doc, 'google.gears.factory.create("beta.localserver")')
    print 'monadsphere.ids["google.localServer"]=' + ls
    return ls

  def getLocalServer(self):
    return LocalServer(self.doc, 'monadsphere.ids["google.localServer"]')

  def createDesktop(self):
    dt = Desktop(self.doc, 'google.gears.factory.create("beta.desktop")')
    print 'monadsphere.ids["google.desktop"]=' + dt
    return dt

  def getDesktop(self):
    return Desktop(self.doc, 'monadsphere.ids["google.desktop"]')

  def createDatabase(self):
    db = GearsDatabase(self.doc, 'google.gears.factory.create("beta.database")')
    print 'monadsphere.ids["google.db"]=' + db
    return db

  def getDatabase(self):
    return GearsDatabase(self.doc, 'monadsphere.ids["google.db"]')

class Desktop(Component):
  def openFiles(self, callBack):
    f = Var(self.doc, 'function(files){')
    callBack(Var(self.doc))
    print '}'
    print self + '.openFiles(' + f + ')'

class LocalServer(Component):
  def createStore(self, name):
    return ResourceStore(self.doc, self + '.createStore(' + StrWrap(name) + ')')

  def openStore(self, name):
    return ResourceStore(self.doc, self + '.openStore(' + StrWrap(name) + ')')

  def removeStore(self, name):
    print self + '.removeStore("' + name + '")'

class ResourceStore(Var):
  pass

class GearsDatabase(Component):

  def open(self, name):
    print self + '.open("' + name + '");'

  def close(self):
    print self + '.close();'

  def execute(self, sqlStatement, argArray = None):
    if (argArray is None):
      result_set = self + '.execute("' + sqlStatement + '")'
    else:
      result_set = self + '.execute("' + sqlStatement + '", ['
      for arg in argArray:
        result_set = result_set + StrWrap(arg) + ','
      result_set = result_set + '])'
    return ResultSet(self.doc, result_set)

  def getLastInsertRowId(self):
    return Var(self.doc, self + '.lastInsertRowId')

class ResultSet(Component):
  def isValidRow(self):
    return BooleanExpr(self.name + '.isValidRow()')

  def next(self):
    return Var(self.doc, self + '.next()')

  def close(self):
    print self + '.close();'

  def fieldCount(self):
    return Var(self.doc, self + '.fieldCount()')

  def fieldName(self, fieldIndex):
    return Var(self.doc, self + '.fieldName(' + str(fieldIndex) + ')')

  def field(self, fieldIndex):
    return Var(self.doc, self + '.field(' + str(fieldIndex) + ')')

  def fieldByName(self, fieldName):
    return Var(self.doc, self + '.fieldByName(' + str(fieldName) + ')')
